package com.nuoniu.sibanyun.service.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.entity.erp.ErpAccount;
import com.nuoniu.sibanyun.entity.finance.VRuleVoucherSetDetail;
import com.nuoniu.sibanyun.entity.finance.VRuleVoucherSetMain;
import com.nuoniu.sibanyun.mapper.finance.VRuleVoucherSetMainMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.finance.IVRuleVoucherSetDetailService;
import com.nuoniu.sibanyun.service.finance.IVRuleVoucherSetFormulaService;
import com.nuoniu.sibanyun.service.finance.IVRuleVoucherSetMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 规则凭证定义主表 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2022-05-06
 */
@Service
public class VRuleVoucherSetMainServiceImpl extends ServiceImpl<VRuleVoucherSetMainMapper, VRuleVoucherSetMain> implements IVRuleVoucherSetMainService {

    @Autowired
    private IErpAccountService accountService;
    @Autowired
    private IVRuleVoucherSetDetailService detailService;
    @Autowired
    private IVRuleVoucherSetFormulaService formulaService;

    @Override
    public Page<VRuleVoucherSetMain> pageList(Page page, VRuleVoucherSetMain ruleVoucherSetMain) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        wrapper.eq("v_type", ruleVoucherSetMain.getVType());
        if(ruleVoucherSetMain.getType() != null) {
            wrapper.eq("type", ruleVoucherSetMain.getType());
        }

        Page<VRuleVoucherSetMain> mainPage =  page(page, wrapper);
        List<VRuleVoucherSetMain> mainList = mainPage.getRecords();
        mainList.forEach(m -> {
            QueryWrapper wrapperDetail = new QueryWrapper();
            wrapperDetail.eq("order_id", m.getId());
             m.setOrderDetailList(detailService.list(wrapperDetail));
        });
        return mainPage;
    }

    @Override
    @Transactional
    public void saveRule(VRuleVoucherSetMain ruleVoucherSetMain) {
        ErpAccount account = accountService.getLastErpAccount();
        UserInfo userInfo = UserThreadLocal.get();
        if(ruleVoucherSetMain.getId() == null) {
            ruleVoucherSetMain.setCompanyId(userInfo.getCompanyId());
            ruleVoucherSetMain.setAccountId(account.getId().intValue());
            ruleVoucherSetMain.setAccountYear(account.getAccountYear());
            ruleVoucherSetMain.setCreateUserId(userInfo.getUserId());
            ruleVoucherSetMain.setCreateUserName(userInfo.getRealName());
        }
        saveOrUpdate(ruleVoucherSetMain);

        //保存明细
        List<Long> detailIds = new ArrayList<>();
        ruleVoucherSetMain.getOrderDetailList().forEach(d -> {
            if(d.getId() != null) {
                detailIds.add(d.getId());
            } else {
                d.setCompanyId(userInfo.getCompanyId());
                d.setOrderId(ruleVoucherSetMain.getId().intValue());
                d.setAccountYear(account.getAccountYear());
            }
        });

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_id", ruleVoucherSetMain.getId());
        if(detailIds.size() > 0) {
            wrapper.notIn("id", detailIds);
        }
        detailService.remove(wrapper);
        detailService.saveOrUpdateBatch(ruleVoucherSetMain.getOrderDetailList());

        //保存公式
        ruleVoucherSetMain.getOrderDetailList().forEach(d -> {
            List<Long> formulaIds = new ArrayList<>();
            if(d.getFormulaList() != null) {
                d.getFormulaList().forEach(f -> {
                    f.setDetailId(d.getId().intValue());
                    if(f.getId() != null) {
                        formulaIds.add(f.getId());
                    }
                });
            }

            QueryWrapper wrapperDelete = new QueryWrapper();
            wrapperDelete.eq("detail_id", d.getId());
            if(formulaIds.size() > 0) {
                wrapperDelete.notIn("id", detailIds);
            }

            formulaService.remove(wrapperDelete);
            if(d.getFormulaList() != null) {
                formulaService.saveOrUpdateBatch(d.getFormulaList());
            }
        });
    }

    @Override
    @Transactional
    public VRuleVoucherSetMain getDetail(Long id) {
        VRuleVoucherSetMain main = this.getById(id);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_id", id);
        List<VRuleVoucherSetDetail> details = detailService.list(wrapper);
        if(details != null) {
            main.setOrderDetailList(details);
            details.forEach(d -> {
                QueryWrapper wrapperFormula = new QueryWrapper();
                wrapperFormula.eq("detail_id", d.getId());
                d.setFormulaList(formulaService.list(wrapperFormula));
            });
        }
        return main;
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_id", id);
        List<VRuleVoucherSetDetail> details = detailService.list(wrapper);
        details.forEach(d -> {
            QueryWrapper wrapperFormula = new QueryWrapper();
            wrapperFormula.eq("detail_id", d.getId());
            formulaService.remove(wrapperFormula);
        });

        QueryWrapper wrapperDetail = new QueryWrapper();
        wrapperDetail.eq("order_id", id);
        detailService.remove(wrapperDetail);
        this.removeById(id);
    }
}
